//
//  EFBChartNavigationView.m
//  Pods
//
//  Created by E.F.B on 15-5-13.
//
//

#import "EFBChartNavigationView.h"
#import <SKInnerShadowLayer.h>
#import <QuartzCore/CALayer.h>

@interface EFBChartNavigationView ()

@property (nonatomic, retain) UIImageView *buttonsView;
@property (nonatomic, retain) NSMutableArray *buttonsArray;
@property (nonatomic, retain) NSArray *buttonTypes;
@property (nonatomic, retain) UILabel *airportLable;
@property (nonatomic, retain) UIImageView *airportView;
@property (nonatomic, retain) UIButton *airportButton;
@property (nonatomic, retain) UIView *buttonsMark;
@property (nonatomic, retain) UIButton *favoriteButton;

@end

@implementation EFBChartNavigationView

- (void)dealloc
{
    [_buttonsArray release];
    [_buttonsView release];
    [_buttonTypes release];
    [_airportLable release];
    [_airportView release];
    [_buttonsMark release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame typeArray:(NSArray *)buttonTypesArray
{
    self= [super initWithFrame:frame];
    if (self) {
        if (buttonTypesArray) {
            self.backgroundColor = [UIColor colorWithRed:48/255.0 green:62/255.0 blue:75/255.0 alpha:1.0f];
            _buttonTypes = [[NSArray alloc] initWithArray:buttonTypesArray];
            
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(8, 8)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.bounds;
            maskLayer.path = maskPath.CGPath;
            self.layer.mask = maskLayer;
            [maskLayer release];

            _airportButton = [UIButton buttonWithType:UIButtonTypeCustom];
            _airportButton.enabled = NO;
            [_airportButton setFrame:CGRectMake(0, 50, frame.size.width, frame.size.width*2)];
            [_airportButton addTarget:self action:@selector(airportButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
            _airportView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"airPort@2x"]];
            [_airportView setFrame:CGRectMake((frame.size.width-60)/2, 0, 60, 60)];
            [_airportButton addSubview:_airportView];

            _airportLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 80, frame.size.width, 30)];
            [_airportLable setBackgroundColor:[UIColor clearColor]];
            [_airportLable setTextAlignment:NSTextAlignmentCenter];
            [_airportLable setFont:[UIFont systemFontOfSize:20]];
            [_airportLable setTextColor:[UIColor whiteColor]];
            _airportLable.adjustsFontSizeToFitWidth = YES;
            [_airportButton addSubview:_airportLable];
            [_airportButton setEnabled:YES];
            [self addSubview:_airportButton];
            
            _favoriteButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [_favoriteButton setFrame:CGRectMake(0, frame.size.height-40, frame.size.width, 40)];
            [_favoriteButton setTitle:NSLocalizedString(@"收藏", @"收藏") forState:UIControlStateNormal];
            [_favoriteButton addTarget:self action:@selector(favoriteButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
            _favoriteButton.titleLabel.font = [UIFont systemFontOfSize:17.0f];
            if (![_favoriteButton.titleLabel.text isEqualToString:@"收藏"]) {
                _favoriteButton.titleLabel.font = [UIFont systemFontOfSize:13.0f];
            }
            _favoriteButton.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
            [self addSubview:_favoriteButton];
            
            // 按钮底图框
            int imageViewWidth = 60;
            int imageViewHeight = 400;
            _buttonsView = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width-imageViewWidth)/2, frame.size.width*3+10, imageViewWidth, imageViewHeight)];
            [_buttonsView setBackgroundColor:[UIColor clearColor]];
            [_buttonsView setClipsToBounds:YES];
            [_buttonsView setUserInteractionEnabled:YES];
            
            SKInnerShadowLayer *innerShadowlayer = [[SKInnerShadowLayer alloc] init];
            innerShadowlayer.frame = CGRectIntegral(_buttonsView.bounds);
            innerShadowlayer.cornerRadius = 7.0f;
            innerShadowlayer.innerShadowOffset = CGSizeMake(0, 0);
            innerShadowlayer.innerShadowRadius = 7.0f;
            
            innerShadowlayer.innerShadowOpacity = 0.8f;
            innerShadowlayer.innerShadowColor = [UIColor lightGrayColor].CGColor;
            
            innerShadowlayer.borderColor = [UIColor colorWithWhite:0.05f alpha:1.0f].CGColor;
            innerShadowlayer.borderWidth = 1.0f;
            
            [_buttonsView.layer addSublayer:innerShadowlayer];
            [_buttonsView.layer setCornerRadius:7.0f];
            [self addSubview:_buttonsView];
            [innerShadowlayer release];
            
            // 添加按钮
            [self addButtons];
//            [self setMark:[_buttonsArray lastObject]];
            self.userInteractionEnabled = YES;
            [self reloadViews];
        }
    }
    return self;
}

- (void)addButtons
{
    CGFloat topMargin = 20;
	CGFloat buttonViewHeight = CGRectGetHeight(_buttonsView.frame) - topMargin*2;
	CGFloat buttonHeight = buttonViewHeight/self.buttonTypes.count;
	CGFloat imageViewWidth = 60;
	_buttonsArray = [[NSMutableArray alloc] init];
	for (int i=0; i<self.buttonTypes.count; i++) {
		UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
		[button setFrame:CGRectMake(0, topMargin+buttonHeight*i, imageViewWidth, buttonHeight)];
		[button setTitle:self.buttonTypes[i] forState:UIControlStateNormal];
		[button setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
		[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateDisabled];
		[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        if ([self.buttonTypes[i] isEqualToString:@"CO"] || [self.buttonTypes[i] isEqualToString:@"黄页"]) {
            [button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
            [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];

            [button.titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
        }
        if ([self.buttonTypes[i] isEqualToString:@"EOSID"]) {
            [button.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
        }
		[button setEnabled:NO];
//        if ([button.titleLabel.text isEqualToString:@"CO"]) {
//            [button setEnabled:YES];
//        }
		[button addTarget:self action:@selector(typeButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
		[_buttonsView addSubview:button];
        [_buttonsArray addObject:button];
    }
}



- (void)setAirportLableText:(NSString *)string
{
    _airportLable.text = string;
}

- (void)clearButtonTypeShowInfo
{
    self.airportLable.text = @"";
    for (UIButton *button in _buttonsArray) {
        [button setEnabled:NO];
        [button setSelected:NO];
    }
    if (_buttonsMark != nil) {
        [_buttonsMark removeFromSuperview];
		_buttonsMark = nil;
    }
}


- (void)airportButtonTapped:(UIButton *)sender
{
    if (_delegate) {
        [_delegate chartAirportButtonClicked:sender];
    }
}

- (void)typeButtonTapped:(UIButton *)sender
{

    if (_delegate) {
        [_delegate chartTypeButtonClicked:sender];
    }
    [self moveMark:(UIButton*)sender];

}

- (void)favoriteButtonTapped:(UIButton *)sender
{
    if (_delegate) {
        [_delegate favoriteButtonClicked:sender];
    }
}

- (void)setMark:(UIButton*)button
{
	if (_buttonsMark != nil) {
		[button setSelected:NO];
		return;
	}
    
	_buttonsMark = [[UIView alloc] initWithFrame:CGRectMake(0, button.frame.origin.y, 5, button.frame.size.height)];
	[_buttonsMark setBackgroundColor:[UIColor colorWithRed:0x06/255.0 green:0xb7/255.0 blue:0x94/255.0 alpha:1.0]];
	[_buttonsView addSubview:_buttonsMark];

    [button setSelected:YES];
}

- (void)moveMark:(UIButton*)button
{
    for(UIButton* button in _buttonsArray)
	{
		[button setSelected:NO];
	}
	[button setSelected:YES];

	float offsetY = (button.frame.origin.y - _buttonsMark.frame.origin.y)>0?3.0:-3.0;
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
	[UIView setAnimationDuration:0.2];
	[_buttonsMark setFrame:CGRectMake(0, button.frame.origin.y + offsetY, 5, button.frame.size.height)];
	[UIView commitAnimations];
	
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
	[UIView setAnimationDuration:0.3];
	[_buttonsMark setFrame:CGRectMake(0, button.frame.origin.y, 5, button.frame.size.height)];
	[UIView commitAnimations];
}

- (void)updateMark:(NSMutableArray *)array
{
    if (_buttonsMark !=nil) {
		[_buttonsMark removeFromSuperview];
		_buttonsMark = nil;
	}
	for (UIButton *button in _buttonsArray)
	{
        [button setEnabled:NO];
//        if ([button.titleLabel.text isEqualToString:@"CO"]) {
//            [button setEnabled:YES];
//            [self setMark:button];
//        }
        [button setSelected:NO];
		for (NSDictionary *dic in array)
		{
			if ([[dic objectForKey:@"typeName"] isEqualToString:button.titleLabel.text])
			{
				[button setEnabled:YES];
				[self setMark:button];
			}
            if ([[dic objectForKey:@"typeName"] isEqualToString:@"STR"] && [button.titleLabel.text isEqualToString:@"STAR"]) {
                [button setEnabled:YES];
				[self setMark:button];
            }
		}
	}
}

- (void)moveMarkForType:(NSString *)type
{
    for (UIButton *button in _buttonsArray)
	{
        [button setSelected:NO];
        if ([button.titleLabel.text isEqualToString:type]) {
            [button setSelected:YES];
            [self moveMark:button];
        }
        if ([type isEqualToString:@"STR"] && [button.titleLabel.text isEqualToString:@"STAR"]) {
            [button setEnabled:YES];
            [self moveMark:button];
        }
	}
}


- (void)reloadViews
{
    if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation))
    {
        [_airportButton setFrame:CGRectMake(0, 20, 69, 110)];
        [_airportLable setFrame:CGRectMake(0,50, 69, 30)];
        [_buttonsView setFrame:CGRectMake(9/2, 69*2-20, 60, 400)];
    }
    else{
        [_airportButton setFrame:CGRectMake(0, 50, 69, 110)];
        [_airportLable setFrame:CGRectMake(0, 80, 69, 30)];
        [_buttonsView setFrame:CGRectMake(9/2, 69*3+10, 60, 400)];
    }
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(8, 8)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
    [maskLayer release];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
